home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / ASL / asltest.c next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  12.7 KB  |  514 lines

  1. ;/* asltest.c - Execute me to compile me with Lattice 5.04
  2. LC -b1 -cfistq -v -y -j73 asltest.c
  3. Blink FROM LIB:c.o,asltest.o TO asltest LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. /*    asltest.c
  8.  *
  9.  *    This test program demonstrates usage of asl.library
  10.  *    File and Font request functions for V2.0 of AmigaDOS.
  11.  *
  12.  *    Revised 90/8/31 by cdh
  13.  *    Revised 90/9/19 by cas
  14.  *    Revised 90/12/19, 91/1/20, 91/1/24, 3/3/91 by cas
  15.  *
  16.  */
  17.  
  18.  
  19. #include <intuition/intuition.h>
  20. #include <utility/tagitem.h>
  21. #include <utility/hooks.h>
  22. #include <libraries/asl.h>
  23.  
  24. #ifdef LATTICE
  25. #include <clib/exec_protos.h>
  26. #include <clib/dos_protos.h>
  27. #include <clib/graphics_protos.h>
  28. #include <clib/intuition_protos.h>
  29. #include <clib/diskfont_protos.h>
  30. #include <clib/asl_protos.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33.  
  34. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  35. int chkabort(void) { return(0); }  /* really */
  36. #endif
  37.  
  38. char VERSTAG[]="\0$VER: asltest 37.2";
  39.  
  40. /***********************************************************************/
  41.  
  42. struct Library  *AslBase;
  43. struct Library  *IntuitionBase;
  44. struct Library    *GfxBase;
  45. struct Library    *DiskfontBase;
  46.  
  47. struct Screen    *MyScreen;
  48. struct Window    *MyWindow;
  49.  
  50. struct TextFont    *MyFont,*OldFont;
  51.  
  52. struct FileRequester *FileRequester;
  53. struct FontRequester *FontRequester;
  54.  
  55. /************************************************************************/
  56.  
  57. void punt( int );
  58. int main(int,char **);
  59.  
  60.  
  61. ULONG FontHook(ULONG, CPTR, struct FontRequester *);
  62.  
  63. ULONG FileHook(ULONG, CPTR, struct FileRequester * );
  64.  
  65. void exit( int );
  66. void printf(char *,...);
  67.  
  68. /************************************************************************/
  69.  
  70. struct TextAttr    MyTextAttr = { "topaz.font",TOPAZ_EIGHTY,0,0 };
  71.  
  72. struct NewScreen MyNewScreen = 
  73.     {
  74.     0,50,320,200,5,
  75.     1,2,
  76.     0,CUSTOMSCREEN,&MyTextAttr,
  77.     "My Custom Screen",
  78.     0,0
  79.     };
  80.  
  81. struct NewWindow MyNewWindow =
  82.     {
  83.     10,10,300,150,
  84.     1,2,
  85.     REFRESHWINDOW|NEWSIZE|ACTIVEWINDOW|CLOSEWINDOW|VANILLAKEY,
  86.     ACTIVATE|SMART_REFRESH|WINDOWDEPTH|WINDOWSIZING|WINDOWDRAG|WINDOWCLOSE,
  87.     0,0,
  88.     "My Window",
  89.     0,            /* Custom screen later...    */
  90.     0,
  91.     20,20,1000,1000,
  92.     CUSTOMSCREEN
  93.     };
  94.  
  95. /************************************************************************/
  96.  
  97.  
  98. struct TagItem MultiTags[] = {
  99.     ASL_FuncFlags,FILF_MULTISELECT | FILF_PATGAD,
  100.     ASL_Pattern,(ULONG)"#?",
  101.     ASL_Hail,(ULONG)"MultiSelect",
  102.     TAG_DONE,
  103.     };
  104.  
  105. struct TagItem SaveTags[] = {
  106.     ASL_Hail,(ULONG)"Save test",
  107.     ASL_FuncFlags,FILF_SAVE | FILF_PATGAD,
  108.     ASL_OKText,(ULONG)"Saveit",
  109.     TAG_DONE,
  110.     };
  111.  
  112. struct TagItem FileTags2[] = {
  113.     ASL_Window,NULL,
  114.     ASL_HookFunc,(ULONG)FileHook,
  115.     ASL_FuncFlags,FILF_DOMSGFUNC | FILF_DOWILDFUNC,
  116.     ASL_Hail,(ULONG)"CustomScreen test",
  117.     TAG_DONE
  118.     };
  119.  
  120. struct TagItem FontTags[] = {
  121.     ASL_FuncFlags,FONF_FIXEDWIDTH | FONF_DOMSGFUNC,
  122.     ASL_MinHeight,8,
  123.     ASL_MaxHeight,9,
  124.     ASL_HookFunc,(ULONG)FontHook,
  125.     TAG_DONE
  126.     };
  127.  
  128. char *ModeList[] = {
  129.     "MyModes",
  130.     "MyJam1",
  131.     "MyJam2",
  132.     "MyComp",
  133.     "MyInverse1",
  134.     "MyInverse2",
  135.     NULL
  136.     };
  137.     
  138.  
  139. struct TagItem FontTags2[] = {
  140.     ASL_Window,NULL,
  141.     ASL_Hail,(ULONG)"ASLib Font Test",
  142.     ASL_OKText,(ULONG)"MyOK",
  143.     ASL_MaxHeight,30,
  144.     ASL_FuncFlags,FONF_STYLES|FONF_BACKCOLOR|FONF_FRONTCOLOR|
  145.             FONF_DRAWMODE|FONF_DOMSGFUNC|FONF_DOWILDFUNC,
  146.     ASL_ModeList,&ModeList,
  147.     TAG_DONE
  148.     };
  149.  
  150. /************************************************************************/
  151.  
  152. main(argc,argv)
  153. int    argc;
  154. char    *argv[];
  155. {
  156. struct IntuiMessage *msg;
  157. ULONG    class,code;
  158.  
  159. struct WBArg    *p_wbargs;
  160. int    i;
  161.  
  162. /* First get some libraries open.    */
  163. IntuitionBase = OpenLibrary("intuition.library",36);
  164. if ( IntuitionBase == NULL ) {
  165.     punt(30);
  166.     }
  167.  
  168. GfxBase = OpenLibrary("graphics.library",36);
  169. if ( GfxBase == NULL ) {
  170.     punt(31);
  171.     }
  172.  
  173. if ( (DiskfontBase = OpenLibrary("diskfont.library",36)) == 0 ) {
  174.     printf("Can't open diskfont.library\n");
  175.     punt(32);
  176.     }
  177.  
  178. if ( (AslBase = OpenLibrary(AslName,36)) == 0 ) {
  179.     printf("Can't open asl.library\n");
  180.     punt(33);
  181.     }
  182.  
  183. /**********************************************************************
  184.  *
  185.  * The following code implements the simplest possible file requester.
  186.  *
  187.  **********************************************************************/
  188.  
  189. FileRequester = AllocFileRequest();
  190. if ( FileRequester == NULL ) {
  191.     printf("Failed to allocate file request\n");
  192.     punt(34);
  193.     }
  194.     
  195. if ( RequestFile(FileRequester) )
  196.     printf("User requested dir (%s) file (%s)\n",FileRequester->rf_Dir,FileRequester->rf_File);
  197. else
  198.     printf("User cancelled filerequest\n");
  199.  
  200. FreeFileRequest( FileRequester );
  201.  
  202. /**********************************************************************
  203.  *
  204.  * Now do another more complex file requester call, with tagitems.
  205.  * This test case allows multiple selections.
  206.  *
  207.  **********************************************************************/
  208.  
  209. FileRequester = AllocAslRequest(ASL_FileRequest, NULL);
  210. if ( FileRequester == NULL ) {
  211.     printf("Failed to allocate file request\n");
  212.     punt(34);
  213.     }
  214.  
  215. if (AslRequest(FileRequester,MultiTags)) {
  216.     if ( i = FileRequester->rf_NumArgs ) {
  217.         printf("Selected %d multiargs\n",i);
  218.         p_wbargs = FileRequester->rf_ArgList;
  219.         while ( i-- ) {
  220.             printf("MultiSel lock $%lx name %s\n",p_wbargs->wa_Lock,p_wbargs->wa_Name);
  221.             p_wbargs++;
  222.             }
  223.         }
  224.     else    printf("No multiarg - dir (%s) file (%s)\n",FileRequester->rf_Dir,FileRequester->rf_File);
  225.     }
  226. else
  227.     printf("User canned file request\n");
  228.  
  229.  
  230. /**********************************************************************
  231.  *
  232.  * Next use the SAME FileRequester structure for a SAVE operation.
  233.  * See the different tag values for info.
  234.  *
  235.  * NOTE, we will eventually call FreeAslRequest() to free FileRequester!
  236.  *
  237.  **********************************************************************/
  238.  
  239. if ( AslRequest( FileRequester, SaveTags ) )
  240.     printf("User requested dir (%s) file (%s)\n",FileRequester->rf_Dir,FileRequester->rf_File);
  241. else
  242.     printf("User cancelled filerequest\n");
  243.  
  244.  
  245. /* You can abort the test after the first file requests by typing
  246.  * something on the command line when you run ASLTEST.
  247.  */
  248. if ( argc > 1 )    punt( 0 );
  249.  
  250.  
  251. /**********************************************************************
  252.  *
  253.  * Next allocate a FontRequester, and make a simple font request.
  254.  *
  255.  **********************************************************************/
  256.  
  257. FontRequester = (struct FontRequester *)AllocAslRequest(ASL_FontRequest,FontTags);
  258.  
  259. if ( FontRequester == NULL ) {
  260.     printf("Failed to allocate font request\n");
  261.     punt(35);
  262.     }
  263.  
  264. if ( AslRequest(FontRequester, NULL) )
  265.     printf("User requested font (%s) size %d style %x flags %x\n    FrontPen: %d BackPen: %d Mode %d\n",
  266.         FontRequester->fo_Attr.ta_Name,
  267.         FontRequester->fo_Attr.ta_YSize,
  268.         FontRequester->fo_Attr.ta_Style,
  269.         FontRequester->fo_Attr.ta_Flags,
  270.         FontRequester->fo_FrontPen,
  271.         FontRequester->fo_BackPen,
  272.         FontRequester->fo_DrawMode );
  273.  
  274. else    printf("CANCELLED:     ");
  275.  
  276. /**********************************************************************
  277.  *
  278.  * Now do some tests on a custom screen.  First, open up
  279.  * a custom screen and window.
  280.  *
  281.  **********************************************************************/
  282.  
  283. MyScreen = OpenScreen(&MyNewScreen);
  284. if ( MyScreen == 0L )
  285.     {
  286.     printf("Can't open custom screen, Oh No!!!!\n");
  287.     punt(1);
  288.     }
  289.  
  290. MyNewWindow.Screen = MyScreen;
  291. MyWindow = OpenWindow(&MyNewWindow);
  292. if ( MyWindow == 0L )
  293.     {
  294.     printf("Can't open custom window, Oh Doubleno!!!\n");
  295.     punt(2);
  296.     }
  297.  
  298. OldFont = MyWindow->RPort->Font;
  299.  
  300.  
  301. /**********************************************************************
  302.  *
  303.  * Do a file request on the custom screen, still using the previously
  304.  * allocated FileRequester.
  305.  *
  306.  **********************************************************************/
  307.  
  308. FileTags2[0].ti_Data = (ULONG)MyWindow;
  309. if ( AslRequest( FileRequester, FileTags2 ) )
  310.     printf("User requested dir (%s) file (%s)\n",FileRequester->rf_Dir,FileRequester->rf_File);
  311. else
  312.     printf("User cancelled filerequest\n");
  313.  
  314.  
  315. /**********************************************************************
  316.  *
  317.  * The following test is a more complex font request test.  Most of
  318.  * this code is not actually related to ASL, but rather is used to
  319.  * open a font and display that font.  The test will loop until the
  320.  * user enters CANCEL.
  321.  *
  322.  **********************************************************************/
  323.  
  324. Again:
  325. FontTags2[0].ti_Data = (ULONG)MyWindow;
  326. if ( ! AslRequest(FontRequester, FontTags2) ) {
  327.     printf("CANCELLED Font Request\n");
  328.     punt(0);
  329.     }
  330.  
  331. printf("User requested font (%s) size %d style %x flags %x\n    FrontPen: %d BackPen: %d Mode %d\n",
  332.     FontRequester->fo_Attr.ta_Name,
  333.     FontRequester->fo_Attr.ta_YSize,
  334.     FontRequester->fo_Attr.ta_Style,
  335.     FontRequester->fo_Attr.ta_Flags,
  336.     FontRequester->fo_FrontPen,
  337.     FontRequester->fo_BackPen,
  338.     FontRequester->fo_DrawMode );
  339.  
  340. ActivateWindow(MyWindow);
  341.  
  342. MyFont = (struct TextFont *)OpenFont(&FontRequester->fo_Attr);
  343.  
  344. if ( MyFont == NULL ) {
  345.     printf("Font not in memory, checking disk...\n");
  346.     }
  347.  
  348. else if ( MyFont->tf_YSize != FontRequester->fo_Attr.ta_YSize )
  349.     {
  350.     printf("Font not of requested size - reopening via disk!!!\n");
  351.     CloseFont(MyFont);
  352.     MyFont = NULL;
  353.     }
  354.  
  355. if ( MyFont == NULL ) {
  356.     MyFont = (struct TextFont *)OpenDiskFont(&FontRequester->fo_Attr);
  357.     if ( MyFont == NULL ) {
  358.         printf("Not on disk either.  Wieird\n");
  359.         punt(51);
  360.         }
  361.     }
  362.  
  363. printf("Got font!\n");
  364.  
  365. Move(MyWindow->RPort,100,100);
  366. SetFont(MyWindow->RPort,MyFont);
  367. SetSoftStyle(MyWindow->RPort,FontRequester->fo_Attr.ta_Style,-1);
  368.  
  369. SetAPen(MyWindow->RPort,FontRequester->fo_FrontPen);
  370. SetBPen(MyWindow->RPort,FontRequester->fo_BackPen);
  371. SetDrMd(MyWindow->RPort,FontRequester->fo_DrawMode);
  372.  
  373. Text(MyWindow->RPort,"Abcdefg!!!12345",15);
  374.  
  375. CloseFont(MyFont);
  376. MyFont = NULL;
  377.  
  378. while ( TRUE ) {
  379.     WaitPort( MyWindow->UserPort );
  380.     msg = (struct IntuiMessage *) GetMsg(MyWindow->UserPort);
  381.     class = msg->Class;
  382.     code = msg->Code;
  383.     ReplyMsg((struct Message *)msg);
  384.     if ( class == CLOSEWINDOW )
  385.         punt( 0 );
  386.     if ( class == VANILLAKEY ) {
  387.         if ( code == 'Q' ) punt(0);
  388.         else goto    Again;
  389.         }
  390.     }
  391.  
  392. }
  393.  
  394.  
  395. /************************************************************************/
  396.  
  397.  
  398. BOOL    FontSelectToggle = TRUE;        /* 
  399.  
  400. /************************************************************************
  401.  *
  402.  *    ULONG FontHook( ULONG Type, CPTR Object, CPTR AslRequester)
  403.  *
  404.  *    This is an ASL_HookFunc which is used to allow the font requester
  405.  *    to interact with the application program.
  406.  *
  407.  *      Note that the DOMSG hook must return the Object (message) pointer
  408.  *      passed to it.  Asl.library will reply the object.
  409.  *    Also note that a font requester DOWILDFUNC must return non-ZERO
  410.  *      to accept a TextAttr.
  411.  *
  412.  ************************************************************************/
  413.  
  414. ULONG FontHook( Type, Object, fr )
  415. ULONG    Type;
  416. CPTR    Object;
  417. struct FontRequester *fr;
  418. {
  419.  
  420. switch ( Type ) {
  421.     case FONF_DOMSGFUNC:
  422.         printf("Font domsgfunc: IDCMP class $%lx\n",((struct IntuiMessage *)Object)->Class);
  423.         return( (ULONG) Object );
  424.         break;
  425.  
  426.     case FONF_DOWILDFUNC:
  427.         printf( "Font dowildfunc: %s %ld\n",
  428.             ((struct TextAttr *)Object)->ta_Name,
  429.             ((struct TextAttr *)Object)->ta_YSize);
  430.  
  431. /* This line would cause every 2nd font to be rejected
  432.          FontSelectToggle = ! FontSelectToggle;
  433. */
  434.         return( (ULONG) FontSelectToggle );
  435.         break;
  436.  
  437.     default:
  438.         printf( "Font hook %ld\n",Type);
  439.     }
  440. }
  441.  
  442. /************************************************************************
  443.  *
  444.  *    ULONG FileHook( ULONG Type, CPTR Object, CPTR AslRequester)
  445.  *
  446.  *    This is an ASL_HookFunc which is used to allow the file requester
  447.  *    to interact with the application program.
  448.  *
  449.  *      Note that the DOMSG hook must return the Object (message) pointer
  450.  *      passed to it.  Asl.library will reply the object.
  451.  *    Also note that the file requester's DOWILDFUNC must return
  452.  *    ZERO to accept a file (opposite of font DOWILDFUNC)
  453.  *
  454.  ************************************************************************/
  455.  
  456. ULONG FileHook( Type, Object, fr )
  457. ULONG    Type;
  458. CPTR    Object;
  459. struct FileRequester *fr;
  460. {
  461. switch ( Type ) {
  462.     case FILF_DOMSGFUNC:
  463.         printf("File domsgfunc: IDCMP class $%lx\n",((struct IntuiMessage *)Object)->Class);
  464.         return( (ULONG) Object );
  465.         break;
  466.  
  467.     case FILF_DOWILDFUNC:
  468.         printf( "File dowildfunc: %s\n",
  469.             ((struct AnchorPath *)Object)->ap_Info.fib_FileName);
  470.         return(NULL);    /* accept the file */
  471.         break;
  472.     default:
  473.         printf( "File hook %ld\n",Type);
  474.     }
  475. }
  476.  
  477. /************************************************************************/
  478. /* punt(rc) is a cleanup function which frees all resources and        */
  479. /*    then exits.                            */
  480. /************************************************************************/
  481.  
  482. void punt(rc)
  483. int    rc;
  484. {
  485. if ( FileRequester ) {
  486.     FreeAslRequest(FileRequester);
  487.     printf("Freed File Request!\n");
  488.     }
  489.  
  490. if ( FontRequester ) {
  491.     FreeAslRequest(FontRequester);
  492.     printf("Freed Font Request!\n");
  493.     }
  494.  
  495. if (MyFont)    CloseFont(MyFont);
  496.  
  497. if (MyWindow) {
  498.     SetFont(MyWindow->RPort,OldFont);
  499.     CloseWindow(MyWindow);
  500.     }
  501. if (MyScreen)    CloseScreen(MyScreen);
  502.  
  503. if (DiskfontBase)    CloseLibrary(DiskfontBase);
  504. if (AslBase)        CloseLibrary(AslBase);
  505. if (GfxBase)        CloseLibrary(GfxBase);
  506. if (IntuitionBase)    CloseLibrary(IntuitionBase);
  507.  
  508. exit (rc);
  509. }
  510.  
  511.  
  512.  
  513.  
  514.